import { Module, NestModule, MiddlewareConsumer } from '@nestjs/common';
import { UserModule } from './modules/user/user.module';
import { LoggerMiddleware } from './middleware/logger.middleware';
import { DateMiddleware } from './middleware/date.middleware';
import { MongooseModule } from '@nestjs/mongoose';
import { funMiddleware } from './middleware/funMiddleware';
import { RoleGuardsModule } from './modules/role-guards/role-guards.module';
import { PipeModule } from './modules/pipe/pipe.module';
import { HttpInterceptorsModule } from './modules/http-interceptors/http-interceptors.module';
import { FilterModule } from './modules/filter/filter.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { DataBase,RegDataBase } from './config/configuration';
console.log('process :>> ', process.env.RUNNING_ENV);
const envFilePath = '.env.' + process.env.RUNNING_ENV;

const DB = TypeOrmModule.forRoot({
  type: 'mysql',
  host: 'localhost',
  port: 3306,
  username: 'root',
  password: 'root',
  database: 'test',
  entities: [],
  synchronize: true,
})
@Module({
  imports: [
    ConfigModule.forRoot({
      envFilePath,
      isGlobal: true,
      load: [DataBase,RegDataBase],
    }),
    UserModule,
    RoleGuardsModule,
    PipeModule,
    HttpInterceptorsModule,
    FilterModule
  ],
  controllers: [],
  providers: [],
})
export class AppModule implements NestModule {
  constructor(private configService: ConfigService) {
    console.log('envFilePath :>> ', envFilePath);
    console.log('DataBase :>> ', DataBase());
    console.log('RegDataBase :>> ', RegDataBase());
    console.log('process.env.DB_HOST :>> ', process.env.DB_HOST);
    console.log('configService DATABASE_USER :>> ', this.configService.get('DB_USERNAME'));
    console.log('configService port :>> ', this.configService.get('port'));
    console.log('configService database.host :>> ', this.configService.get('database.host'));
    console.log('configService regdatabase.port :>> ', this.configService.get('regdatabase.port'));
  }
  configure(consumer: MiddlewareConsumer) {
    consumer
      .apply(LoggerMiddleware, DateMiddleware, funMiddleware)
      .forRoutes('');
  }
}
